import os
def find_files_with_extension(directory, extension):
    file_list=[]
    try:
        for root,dirs,files in os.walk(directory):
            for file in files:
                if file.endswith(extension):
                    file_path=os.path.join(root,file)
                    file_list.append(file_path)  
    except:
        print("错误")
    return file_list
directory='C:\\Users\\pc\\Desktop\\大二'
extension='.txt'
print(find_files_with_extension(directory,extension))